package com.chuanyang.driverh.mainorder.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.bean.AssignGoodData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.widget.SwipeLayout;

import java.util.ArrayList;
import java.util.List;

public class AssignGoodsAdapter extends BaseListAdapter<AssignGoodData> {
    private ArrayList<SwipeLayout> openedItems = new ArrayList<SwipeLayout>();

    private List list;
    private Context context;
    private static GoodInfoHolder holder;
//   private int count=0;

    public AssignGoodsAdapter(Context context, List<AssignGoodData> list) {

        super(context, list);
        this.list=list;
        this.context=context;
    }


    @Override
    public View getZkView(final int position, View convertView, ViewGroup parent) {
     if (convertView==null)
     {
         convertView= View.inflate(context, R.layout.order_item_assigngoods,null);
     }
        //获取每个条目要展示的数据
        AssignGoodData mitem = getItem(position);
        //获取Viewholder
        holder=  GoodInfoHolder.getHolder(convertView);
        BitmapUtil.showImgForNetPath(mitem.getDriverImage(),holder.iv_assigngoods_drivericon);
//        YncApplication.getInstance().loadCircleImage(context, mitem.getDriverImage(), R.drawable.shengfeng, R.drawable.shengfeng, holder.iv_assigngoods_drivericon);
        holder.tv_assigngoods_drivername.setText(mitem.getDriverName());
      //  holder.tv_assigngoods_goodname.setText("钢，铁，铜");//货物名称 此处多个
        holder.tv_assigngoods_weight.setText("已分配："+mitem.getAssignWeight());
        holder.tv_assign_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(listener!=null)
                {
                    listener.onCustomerListener(holder.tv_assign_delete,position);
                }

            }
        });
       holder.tv_assign_change.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if(listener!=null)
               {
                   listener.onCustomerListener(holder.tv_assign_change,position);
               }

           }
       });
        return convertView;
    }
    public void closeAllItems() {
        // 关闭当前所有打开的条目
        for (int i = 0; i < openedItems.size(); i++) {
            openedItems.get(i).close();
        }

        // 集合清除
        openedItems.clear();
    }
    class MySwipeListener implements SwipeLayout.OnSwipeListener {

        @Override
        public void onOpen(SwipeLayout layout) {
            // 记录当前被开打的条目
            openedItems.add(layout);
        }

        @Override
        public void onClose(SwipeLayout layout) {
            // 关闭了就从集合中移除
            openedItems.remove(layout);
        }

        @Override
        public void onStartOpen(SwipeLayout layout) {
            // 新的条目将要打开
            closeAllItems();
        }

        @Override
        public void onStartClose(SwipeLayout layout) {

        }
    }
     static class  GoodInfoHolder
    {
            private TextView tv_assigngoods_drivername,tv_assigngoods_goodname,tv_assigngoods_weight,tv_assign_delete,tv_assign_change;
            private ImageView iv_assigngoods_drivericon;
             private SwipeLayout sl_assign;
        public GoodInfoHolder(View converView)
        {
            iv_assigngoods_drivericon = (ImageView) converView.findViewById(R.id.iv_assigngoods_drivericon);
            tv_assigngoods_drivername = (TextView) converView.findViewById(R.id.tv_assigngoods_drivername);
          //  tv_assigngoods_goodname = (TextView)converView.findViewById(R.id.tv_assigngoods_goodname);
            tv_assigngoods_weight = (TextView)converView.findViewById(R.id.tv_assigngoods_weight);
            tv_assign_change = (TextView)converView.findViewById(R.id.tv_assign_change);
            tv_assign_delete = (TextView)converView.findViewById(R.id.tv_assign_delete);
            sl_assign= (SwipeLayout) converView.findViewById(R.id.sl_assign);
        }
        public  static GoodInfoHolder getHolder(View converView)
        {
            holder = (GoodInfoHolder) converView.getTag();
            if(holder ==null)
            {
                holder =new GoodInfoHolder(converView);
                converView.setTag(holder);
            }
            return holder;
        }
    }
}
